home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / gi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  6.5 KB  |  300 lines

  1. /*
  2.  * $RCSfile: gi.cpp $
  3.  *
  4.  * $Author: marcel $
  5.  *
  6.  * $Revision: 1.6 $
  7.  *
  8.  * $Date: 1995/03/31 00:49:32 $
  9.  *
  10.  * $Locker: marcel $
  11.  *
  12.  * $State: Exp $
  13.  *
  14.  * Amiga version
  15.  *
  16.  * Copyright © 1995 Marcel Offermans
  17.  *
  18.  * tabsize = 5
  19.  */
  20.  
  21. /* includes */
  22. #include <exec/types.h>
  23. #include <proto/graphics.h>
  24. #include <proto/intuition.h>
  25. #include <proto/diskfont.h>
  26. #include <proto/asl.h>
  27. #include <graphics/gfxmacros.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "gi.h"
  32.  
  33. /* prototypes */
  34. int round(double);
  35.  
  36. /* macros */
  37. #define RGB24(r,g,b) (r << 24),(g << 24),(b << 24)
  38. #define LINESEG_LENGTH 10
  39.  
  40. /* size of the font in pixels: you can't change this without seriously messing up the display */
  41. const int                    CHR_HGT_PIX        = 10;
  42. const int                    CHR_WID_PIX        = 9;
  43.  
  44. /* globals */
  45. BOOL                        available            = FALSE;
  46. double                    SCALE;
  47. double                    CHR_HGT;
  48. double                    CHR_WID;
  49. static int                maxx, maxy;
  50. static int                tran_table[]        = {8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7};
  51. struct Screen *            screen_ptr        = NULL;
  52. struct Window *            window_ptr        = NULL;
  53. struct RastPort *            rp_ptr            = NULL;
  54. PLANEPTR                    plane_ptr            = NULL;
  55. USHORT                    areapattern_ptr[]    = {0x0000};
  56. struct TextAttr            textattr            = {"topaz.font", 8, FS_NORMAL, FPF_ROMFONT};
  57. struct TmpRas                tmpras;
  58. struct ScreenModeRequester *    smreq_ptr            = NULL;
  59. ULONG                    colortable[]        = {16L<<16 + 0,
  60.     RGB24(064, 064, 064),
  61.     RGB24(255, 000, 000),
  62.     RGB24(000, 255, 000),
  63.     RGB24(255, 255, 000),
  64.     RGB24(000, 000, 255),
  65.     RGB24(255, 000, 255),
  66.     RGB24(000, 255, 255),
  67.     RGB24(255, 255, 255),
  68.     RGB24(000, 000, 000),
  69.     RGB24(128, 000, 000),
  70.     RGB24(000, 128, 000),
  71.     RGB24(128, 128, 000),
  72.     RGB24(000, 000, 128),
  73.     RGB24(128, 000, 128),
  74.     RGB24(000, 128, 128),
  75.     RGB24(128, 128, 128),
  76.     0};
  77. struct colors            car_clrs[] = {
  78.     {oWHITE,        oWHITE},
  79.     {oRED,        oWHITE},
  80.     {oBLACK,        oYELLOW},
  81.     {oGREEN,        oWHITE},
  82.     {oWHITE,        oMAGENTA},
  83.     {oBLUE,        oBLUE},
  84.     {oBLUE,        oWHITE},
  85.     {oYELLOW,        oYELLOW},
  86.     {oRED,        oRED},
  87.     {oBROWN,        oCYAN},
  88.     {oCYAN,        oCYAN},
  89.     {oBLACK,        oLIGHTMAGENTA},
  90.     {oBLACK,        oBLACK},
  91.     {oGREEN,        oGREEN},
  92.     {oBROWN,        oYELLOW},
  93.     {oMAGENTA,    oLIGHTGREEN},
  94. };
  95.  
  96. /* closes the graphical display screen */
  97. void resume_normal_display(void)
  98. {
  99.     /* reset the available flag to suppress graphics output */
  100.     available = FALSE;
  101.  
  102.     if (smreq_ptr)
  103.     {
  104.         FreeAslRequest(smreq_ptr);
  105.         smreq_ptr = NULL;
  106.     }
  107.     if (plane_ptr)
  108.     {
  109.         FreeRaster(plane_ptr, window_ptr->Width, window_ptr->Height);
  110.         plane_ptr = NULL;
  111.     }
  112.     if (window_ptr)
  113.     {
  114.         CloseWindow(window_ptr);
  115.         window_ptr = NULL;
  116.     }
  117.     if (screen_ptr)
  118.     {
  119.         CloseScreen(screen_ptr);
  120.         screen_ptr = NULL;
  121.     }
  122. }
  123.  
  124. long xcon(double x)
  125. {
  126.    return((long)(round(x / SCALE)));
  127. }
  128.  
  129. long ycon(double y)
  130. {
  131.    return((long)(maxy - round(y / SCALE)));
  132. }
  133.  
  134. void draw_line(double x0, double y0, double x1, double y1)
  135. {
  136.     if (!available)
  137.     {
  138.         return;
  139.     }
  140.  
  141.     Move(rp_ptr, xcon(x0), ycon(y0));
  142.     Draw(rp_ptr, xcon(x1), ycon(y1));
  143. }
  144.  
  145. void rectangle(double ulx, double uly, double lrx, double lry)
  146. {
  147.     if (!available)
  148.     {
  149.         return;
  150.     }
  151.  
  152.     RectFill(rp_ptr, xcon(ulx), ycon(uly), xcon(lrx), ycon(lry));
  153. }
  154.  
  155. void set_color(int our_color)
  156. {
  157.     if (!available)
  158.     {
  159.         return;
  160.     }
  161.  
  162.     SetAPen(rp_ptr, tran_table[our_color]);
  163. }
  164.  
  165. void set_fill_color(int our_color)
  166. {
  167.     if (!available)
  168.     {
  169.         return;
  170.     }
  171.  
  172.     SetBPen(rp_ptr, tran_table[our_color]);
  173. }
  174.  
  175. void flood_fill(double X, double Y)
  176. {
  177.     if (!available)
  178.     {
  179.         return;
  180.     }
  181.  
  182.     Flood(rp_ptr, 1, xcon(X), ycon(Y));
  183. }
  184.  
  185. void text_output(double X, double Y, char *source)
  186. {
  187.     if (!available)
  188.     {
  189.         return;
  190.     }
  191.  
  192.     SetDrMd(rp_ptr, JAM1);
  193.     Move(rp_ptr, xcon(X), ycon(Y) + 6);
  194.     Text(rp_ptr, source, strlen(source));
  195.     SetDrMd(rp_ptr, JAM2);
  196. }
  197.  
  198. void initialize_graphics(void)
  199. {
  200.     BOOL result;
  201.  
  202.     /* ask the user for a screenmode */
  203.     if (smreq_ptr = (struct ScreenModeRequester *)AllocAslRequest(ASL_ScreenModeRequest, NULL))
  204.     {
  205.         result = AslRequestTags(smreq_ptr,
  206.             ASLSM_PrivateIDCMP,            TRUE,
  207.             ASLSM_TitleText,            "Select a screenmode for RARS screen...",
  208.             ASLSM_PositiveText,            "Use",
  209.             ASLSM_NegativeText,            "Default",
  210.             ASLSM_InitialDisplayDepth,    4,
  211.             ASLSM_InitialDisplayWidth,    640,
  212.             ASLSM_InitialDisplayHeight,    512,
  213.             ASLSM_InitialDisplayID,        HIRESLACE_KEY,
  214.             ASLSM_InitialOverscanType,    OSCAN_TEXT,
  215.             ASLSM_InitialHeight,        400,
  216.             ASLSM_DoWidth,                TRUE,
  217.             ASLSM_DoHeight,            TRUE,
  218.             ASLSM_DoDepth,                TRUE,
  219.             ASLSM_DoOverscanType,        TRUE,
  220.             ASLSM_DoAutoScroll,            TRUE,
  221.             TAG_DONE);
  222.     }
  223.  
  224.     /* open a screen */
  225.     if (screen_ptr = OpenScreenTags(NULL,
  226.         SA_Depth,            (result) ? (smreq_ptr->sm_DisplayDepth) : (4),
  227.         SA_Width,            (result) ? (smreq_ptr->sm_DisplayWidth) : (640),
  228.         SA_Height,        (result) ? (smreq_ptr->sm_DisplayHeight) : (512),
  229.         SA_DisplayID,        (result) ? (smreq_ptr->sm_DisplayID) : (HIRESLACE_KEY),
  230.         SA_Overscan,        (result) ? (smreq_ptr->sm_OverscanType) : (OSCAN_TEXT),
  231.         SA_AutoScroll,        (result) ? (smreq_ptr->sm_AutoScroll) : (TRUE),
  232.         SA_Type,            CUSTOMSCREEN | SCREENQUIET,
  233.         SA_ShowTitle,        FALSE,
  234.         SA_Colors32,        colortable,
  235.         SA_Font,            &textattr,
  236.         TAG_DONE))
  237.     {
  238.         if (window_ptr = OpenWindowTags(NULL,
  239.             WA_CustomScreen,    screen_ptr,
  240.             WA_Backdrop,        TRUE,
  241.             WA_Borderless,        TRUE,
  242.             WA_Activate,        TRUE,
  243.             WA_IDCMP,            IDCMP_VANILLAKEY,
  244.             TAG_DONE))
  245.         {
  246.             /* set the rastport pointer */
  247.             rp_ptr = window_ptr->RPort;
  248.  
  249.             /* try to allocate a bitplane for the flood filling operation */
  250.             if (plane_ptr = AllocRaster(window_ptr->Width, window_ptr->Height))
  251.             {
  252.                 InitTmpRas(&tmpras, plane_ptr, RASSIZE(window_ptr->Width, window_ptr->Height));
  253.                 rp_ptr->TmpRas = &tmpras;
  254.                 /* set up the patterns for the pens */
  255.  
  256.                 /* jam 2 colors onto the screen when drawing */
  257.                 SetDrMd(rp_ptr, JAM2);
  258.  
  259.                 /* set the area fill pattern */
  260.                 SetAfPt(rp_ptr, areapattern_ptr, 0);
  261.  
  262.                 /* set the pen that is used for lines and arcs to initial color */
  263.                 SetAPen(rp_ptr, 1);
  264.  
  265.                 /* set the pen that is used for filling areas to initial color */
  266.                 SetBPen(rp_ptr, 2);
  267.  
  268.                 /* set up some variables that are needed for the feet to pixel conversions */
  269.                 maxx = window_ptr->Width - 1;
  270.                 maxy = window_ptr->Height - 1;
  271.  
  272.                 /* determine the distance in feet that each pixel will represent */
  273.                 SCALE = X_MAX / (double)maxx;
  274.                 if (Y_MAX / (double)maxy > SCALE)
  275.                 {
  276.                     SCALE = Y_MAX / (double)maxy;
  277.                     X_MAX = maxx * SCALE;
  278.                 }
  279.                 else
  280.                 {
  281.                     Y_MAX = maxy * SCALE;
  282.                 }
  283.  
  284.                 /* compute character width and height in feet */
  285.                 CHR_HGT = CHR_HGT_PIX * SCALE;
  286.                 CHR_WID = CHR_WID_PIX * SCALE;
  287.  
  288.                 /* set the IDCMP bit mask */
  289.                 idcmpmask = 1L << window_ptr->UserPort->mp_SigBit;
  290.  
  291.                 /* add an exit trap which cleans up this screen and window */
  292.                 atexit(resume_normal_display);
  293.  
  294.                 /* set the available flag so the GI knows it can draw */
  295.                 available = TRUE;
  296.             }
  297.         }
  298.     }
  299. }
  300.